$(function () {
    var url = "/except/usercontroller/getUserProxyInfo.action";
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var jsonstr = "{token:" + token + "}";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 20000) {
                $("#headimg").attr("src", data.data.headimg);
                $("#nickname").html(data.data.nickname);
                $("#sparemoney").html(data.data.sparemoney);
                $("#firstmoney").html(data.data.first + "元");
                $("#secondmoney").html(data.data.second + "元");
                $("#thirdmoney").html(data.data.third + "元");
                $("#forthmoney").html(data.data.forth + "元");

                var type = data.data.type;
                if(type == 1) {
                    $("#proxytype").html("普通代理");
                    $("#proxyimg").attr("src","img/v1.png");
                } else if(type == 2) {
                    $("#proxytype").html("银牌代理");
                    $("#proxyimg").attr("src","img/v2.png");
                    $("#firstdesc").hide();
                } else if(type == 3) {
                    $("#proxytype").html("金牌代理");
                    $("#proxyimg").attr("src","img/v3.png");
                    $("#firstdesc").hide();
                    $("#seconddesc").hide();
                } else if(type == 4) {
                    $("#proxytype").html("砖石代理");
                    $("#proxyimg").attr("src","img/v4.png");
                    $("#firstdesc").hide();
                    $("#seconddesc").hide();
                    $("#thirddesc").hide();
                } else if(type == 5) {
                    $("#proxytype").html("至尊代理");
                    $("#proxyimg").attr("src","img/v5.png");
                    $("#firstdesc").hide();
                    $("#seconddesc").hide();
                    $("#thirddesc").hide();
                    $("#forthdesc").hide();
                } else {
                    $("#proxyimg").remove();
                }

                remove_advertising();
            } else if (code == 10001 || code == 10002) {
                location.href = "/except/login.jsp";
            } else {
                $("#hintbg").show();
                $("#hint_message").html(message);
            }
        },
        error: function (data) {
            $("#hintbg").show()
            $("#hint_message").html("网络异常");
        }
    });
});


var payflag = false;

function pay(btnid) {
    var requrl = location.href.split('#')[0];
    $("#background").hide();
    if(payflag)
        return;
    payflag = false;
    var url = "/except/indentcontroller/recharge.action";
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var jsonstr = "{token:" + token + "}";
    var paytype = $("#paytype :radio:checked").val();

    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            paytype: paytype,
            bntid:btnid
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 20000) {
                confirm = true;
                $("#nonce_str").val(data.data.nonce_str);
                $("#result_sign").val(data.data.result_sign);
                $("#timestamp").val(data.data.indenttime);
                $("#prepay_id").val(data.data.prepay_id);
                $("#payinfo").submit();
                //TODO 判断支付类型，并执行相关操作
                // if(paytype == 1)
                //     location.href = "";
                // else {
                //
                //     //跳转至微信支付
                // }
            } else if (code == 10001 || code == 10002) {
                location.href = "/except/login.jsp";
            } else {
                $("#hintbg").show();
                $("#hint_message").html(message);
            }
        },
        error: function (data) {
            $("#hintbg").show();
            $("#hint_message").html("网络异常");
        }
    });
}